package com.ttc.mBackup.BackupContacts;

import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.ttc.mBackup.R;



public class BackupContactActivity extends Activity {
	CustomListAdapter adapter;
	private ListView listView;
	private EditText txtFileName;
	
	String fileName = "mBackup_";
	private Button btnBackup;
	private Button btnCancel;
	private ImageButton btnCheckAll;
	private ImageButton btnUnCheckAll;
	ArrayList<Contact>contacts;
	ArrayList<Contact>contactsForBackup=new ArrayList<Contact>();
	private EditText edtNote;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.c_backup_layout);
		
		listView = (ListView) findViewById(R.id.listViewContact);
		
		ContactDAO cd=new ContactDAO(this.getContentResolver());
		contacts=cd.readAllContacts();
		if(contacts==null){
			Toast.makeText(getApplicationContext(),
					"Nocontact to backup",
					Toast.LENGTH_SHORT).show();
			return;
		}
		// create file name
		Date d = new Date();
		
		String s = DateFormat.format("MMMMdyyyy_hhmmss ", d.getTime())
				.toString();
		fileName += s.trim();
		Log.i("filename", "" + fileName);

		txtFileName=(EditText) findViewById(R.id.c_txt_filename);
		txtFileName.setText(fileName);
		
		adapter=new CustomListAdapter(this, R.id.listViewContact, contacts);
		listView.setAdapter(adapter);
		btnBackup = (Button) findViewById(R.id.c_btnBackup);
		btnCancel=(Button) findViewById(R.id.c_btnCancel);
		btnCheckAll=(ImageButton) findViewById(R.id.c_ibtnCheckall);
		btnUnCheckAll=(ImageButton) findViewById(R.id.c_ibtnUnCheckall);
		
		btnBackup.setOnClickListener(oclBackup);
		btnCancel.setOnClickListener(oclCancel);
		btnCheckAll.setOnClickListener(oclCheckAll);
		btnUnCheckAll.setOnClickListener(oclUnCheckAll);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 0, "Check all").setIcon(R.drawable.checkicon);
		menu.add(0, 2, 0, "Uncheck all").setIcon(R.drawable.uncheckicon);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			for(Contact contact:contacts){
				contact.setChecked(true);
				Log.i("checked?", "" + contact.isChecked());
			}
			adapter=new CustomListAdapter(this, R.id.listViewContact, contacts);
			listView.setAdapter(adapter);
			adapter.notifyDataSetChanged();
			break;
		
		case 2:
			for(Contact contact:contacts){
				contact.setChecked(false);
				Log.i("checked?", "" + contact.isChecked());
			}
			//adapter=new CustomListAdapter(this, R.id.listViewContact, contacts);
			listView.setAdapter(adapter);
			adapter.notifyDataSetChanged();
			break;
		}
		return true;
	}

	OnClickListener oclCheckAll=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			for(Contact contact:contacts){
				contact.setChecked(true);
				Log.i("checked?", "" + contact.isChecked());
			}
			adapter=new CustomListAdapter(BackupContactActivity.this, R.id.listViewContact, contacts);
			listView.setAdapter(adapter);
			adapter.notifyDataSetChanged();
			
		}
	};
	
	OnClickListener oclUnCheckAll=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			for(Contact contact:contacts){
				contact.setChecked(false);
				Log.i("checked?", "" + contact.isChecked());
			}
			//adapter=new CustomListAdapter(this, R.id.listViewContact, contacts);
			listView.setAdapter(adapter);
			adapter.notifyDataSetChanged();
			
		}
	};
	
	OnClickListener oclBackup=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			AlertDialog.Builder builder = new AlertDialog.Builder(
					BackupContactActivity.this);
			builder.setMessage("Are you sure you want to backup?");
			builder.setCancelable(false);
			builder.setPositiveButton("Yes",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							Log.i("contacts size", "" + contacts.size());
							for(Contact contact:contacts){
								
								if(contact.isChecked()==true)
								contactsForBackup.add(contact);
							}
							edtNote=(EditText) findViewById(R.id.c_edtNote);
							Date d = new Date();
							String backupTime = DateFormat.format(
									"MM dd, yyyy hh:mm:ss ", d.getTime())
									.toString();
							ContactBackup cb = new ContactBackup();
							cb.setContacts(contactsForBackup);
							cb.setNote(edtNote.getText().toString());
							cb.setBackupTime(backupTime);
							
							fileName=txtFileName.getText().toString()+ ".json"; 
							if (FileProvider.writeFileToSDCard(cb,fileName)) {
								Toast.makeText(getApplicationContext(),
										"Backup successfully",
										Toast.LENGTH_SHORT).show();
							} else {
								Toast.makeText(getApplicationContext(),
										"Not success", Toast.LENGTH_SHORT)
										.show();
							}
						}

					});
			builder.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
			AlertDialog alert = builder.create();
			alert.show();
			
		}
	};

	OnClickListener oclCancel=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			BackupContactActivity.this.finish();
			
		}
	};
}
